package cs2340.flyingkiwi.todoapp;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * FlyingkiwiToDoListActivity class
 * @author Group Flying Kiwi
 * @version 15 Mar 2012
 */
public class CS2340ToDoAppActivity extends Activity 
{
	private ProgramManager pManager;
	private ToDoAppDbAdapter mDbHelper;
	
	public CS2340ToDoAppActivity(){
		pManager = new ProgramManager();
	}
	
	
    /** Called when the activity is first created. */

	/**
	 * onCreate method
	 * @param savedInstanceState
	 */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("HELLO ACTIVITY", "In the main");
        mDbHelper = new ToDoAppDbAdapter(this);
        mDbHelper.open();
    }
    
    /**
     * Navigates to sign up page when sign up button is clicked.
     * @param view
     */
    public void signUpPage(View view) 
    {
    	Log.d("CS2340 signUpPage", "Before Creating Intent");
    	Intent i = new Intent(this, EditAccount.class);        
    	Log.d("CS2340 signUpPage", "After Creating Intent");
    	startActivityForResult(i, 0);  
		//setContentView(R.layout.signup);
	}
    
    /**
     * Navigates back to main page when cancel button is clicked.
     * @param view
     */
    /*public void cancelSignUpPage(View view) 
    {
		Log.d("HELLO ACTIVITY", "Handling the cancel button");
		setContentView(R.layout.main);
    }*/
    
    /**
     * When create account button is pushed, takes in info from text 
     * fields and checks the info with ProgramManager. Navigates to main 
     * if the info is correct and displays error text if info is incorrect.
     * @param view
     */
    /*public void createAccountPage(View view) 
    {
    	
		Log.d("HELLO ACTIVITY", "Creating an account");
		EditText username = (EditText)findViewById(R.id.username);
		EditText password1 = (EditText)findViewById(R.id.password1);
		EditText password2 = (EditText)findViewById(R.id.password2);
		EditText name = (EditText)findViewById(R.id.name);
		EditText email = (EditText)findViewById(R.id.email);
		if(pManager.createAccount(username.getText().toString(), 
				password1.getText().toString(), password2.getText().toString(), 
				name.getText().toString(), email.getText().toString()))
			setContentView(R.layout.main);
		else
		{
			TextView signUpError = (TextView)findViewById(R.id.signUpError);
			signUpError.setText("Incomplete or Incorrect Form");
		}
    }*/
    
    /**
     * When login button is clicked, takes in info from text fields
     * and checks the login with ProgramManager. Begins AccountManager
     * Activity if it is a good login, otherwise displays an error.
     * @param view
     */
    public void loginPage(View view) 
    {
		EditText username = (EditText)findViewById(R.id.usernameField);
		EditText password = (EditText)findViewById(R.id.passwordField);
		Log.d("CS2340 loginPage", "Before fetching account");
		Cursor accountCursor = mDbHelper.fetchAccount(username.getText().toString());
		if(accountCursor.getCount() > 0)
		{	
			startManagingCursor(accountCursor);
			accountCursor.moveToFirst();
			Long rowId = accountCursor.getLong(0);
			if(accountCursor.getString(2).equals(password.getText().toString()))
			{
				Intent i = new Intent(this, AccountManager.class);
				i.putExtra(ToDoAppDbAdapter.KEY_ACCOUNT_ROWID, rowId.toString());
				startActivityForResult(i, 0);
				Toast.makeText(CS2340ToDoAppActivity.this, " WELCOME!", Toast.LENGTH_LONG).show();
			}
		}
		else
		{
			TextView badLogin = (TextView)findViewById(R.id.loginText);
			badLogin.setText("Bad Login :(");
		}
    }
    
    /**
     * Interface definition for a class that will be called 
     * when the container's activity receives an activity result.
     * @param requestCode
     * @param resultCode
     * @param intent
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}